<?php
/**
 * This is a quick-and-dirty draft QPanel object to do Create, Edit, and Delete functionality
 * of the Sample class.  It uses the code-generated
 * SampleMetaControl class, which has meta-methods to help with
 * easily creating/defining controls to modify the fields of a Sample columns.
 *
 * Any display customizations and presentation-tier logic can be implemented
 * here by overriding existing or implementing new methods, properties and variables.
 *
 * NOTE: This file is overwritten on any code regenerations.  If you want to make
 * permanent changes, it is STRONGLY RECOMMENDED to move both sample_edit.php AND
 * sample_edit.tpl.php out of this Form Drafts directory.
 *
 * @package My QCubed Application
 * @subpackage Drafts
 */
class SampleEditPanel extends QPanel {
    // Local instance of the SampleMetaControl
    public $mctSample;
    public $objSample;

    // Controls for Sample's Data Fields
    public $lblId;
    public $txtaOrg;
    public $txtaOracle;

    public $lstSchool;
    public $chkReturnable;
    public $calDate;


    // Other ListBoxes (if applicable) via Unique ReverseReferences and ManyToMany References
    public $lstPeopleAsReceiving;

    // Other Controls
    public $btnSave;
    public $btnDelete;
    public $btnCancel;

    // Callback
    protected $strClosePanelMethod;
    public function txtAOrg_Change($strParameter) {
        $objMemberArray = Org::QueryArray(
                QQ::AndCondition(
                QQ::Equal(QQN::Org()->TypeId, 1),
                QQ::Like(QQN::Org()->Name, $strParameter . '%')
                ),
                QQ::Clause(QQ::OrderBy(QQN::Org()->Name))

        );
        $result = array();
        foreach($objMemberArray as $objMember) {
            $result[] = $objMember->Name;
        }
        return $result;
    }

    public function __construct($objParentObject, $mctSample, $strControlId = null) {
        // Call the Parent
        try {
            parent::__construct($objParentObject, $strControlId);
        } catch (QCallerException $objExc) {
            $objExc->IncrementOffset();
            throw $objExc;
        }

        // Setup Callback and Template
        $this->strTemplate = __DOCROOT__ . __SAMPLES__ . '/tpl/QePanel_edit.tpl.php';

        $this->mctSample = SampleMetaControl::CreateFromPathInfo($this);

        $this->txtaOrg = $this->mctSample->txtaOrg_Create();
        $this->chkReturnable = $this->mctSample->chkReturnable_Create();
        $this->chkReturnable->SetParentControl($this);

        $this->calDate = $this->mctSample->calDate_Create();
        $this->calDate->SetParentControl($this);

        $this->lstPeopleAsReceiving = $this->mctSample->lstPeopleAsReceiving_Create();
        $this->lstPeopleAsReceiving->SetParentControl($this);
        // Create Buttons and Actions on this Form
        $this->btnSave = new QButton($this);
        $this->btnSave->Text = QApplication::Translate('Save');
        $this->btnSave->Name = QApplication::Translate('Save');
        $this->btnSave->AddAction(new QClickEvent(), new QAjaxControlAction($this, 'btnSave_Click'));
        $this->btnSave->CausesValidation = $this;

        $this->btnCancel = new QButton($this);
        $this->btnCancel->Text = QApplication::Translate('Cancel');
        $this->btnCancel->Name = QApplication::Translate('Cancel');
        $this->btnCancel->AddAction(new QClickEvent(), new QAjaxControlAction($this, 'btnCancel_Click'));

        $this->btnDelete = new QButton($this);
        $this->btnDelete->Text = QApplication::Translate('Delete');
        $this->btnDelete->Name = QApplication::Translate('Delete');

        $this->btnDelete->AddAction(new QClickEvent(), new QConfirmAction(QApplication::Translate('Are you SURE you want to DELETE this') . ' ' . QApplication::Translate('Sample') . '?'));
        $this->btnDelete->AddAction(new QClickEvent(), new QAjaxControlAction($this, 'btnDelete_Click'));
        //	$this->btnDelete->Visible = $this->mctSample->EditMode;
    }

    // Control AjaxAction Event Handlers
    public function btnSave_Click($strFormId, $strControlId, $strParameter) {
        // Delegate "Save" processing to the SampleMetaControl
        $blnEdit= $this->mctSample->__get("EditMode");
        $this->mctSample->SaveSample();
        if (!$blnEdit) {
            $this->objSample= $this->mctSample->__get("Sample");
            QApplication::Redirect(__SAMPLES__ . "/sample_edit.php/".$this->objSample->Id);
        }
        else {
            QApplication::Redirect(__SAMPLES__ );
        }
        //$this->CloseSelf(true);
    }
    
    public function btnDelete_Click($strFormId, $strControlId, $strParameter) {
        // Delegate "Delete" processing to the SampleMetaControl
        $this->mctSample->DeleteSample();
        //$this->CloseSelf(true);
    }

    public function btnCancel_Click($strFormId, $strControlId, $strParameter) {
        QApplication::Redirect(__SAMPLES__ );
    }

    // Close Myself and Call ClosePanelMethod Callback
    protected function CloseSelf($blnChangesMade) {
        $strMethod = $this->strClosePanelMethod;
        $this->objForm->$strMethod($blnChangesMade);
    }


}
?>